<route lang="json">
{
    "name": "userManage",
    "meta": {
        "title": "控制台-用户管理",
        "requiresAuth": true
    }
}
</route>

<script setup lang="ts">
import { ElMessage } from 'element-plus'
import { computed, onMounted, reactive, ref } from 'vue'
import { getUserList } from '~/api/user'

// 定义用户接口类型
interface User {
  userid: string
  username: string
  password?: string
  email?: string
  nickname?: string
  qq?: string
  wx?: string
  avatar?: string
  sex?: number
  birthday?: string
  [key: string]: any
}

// 用户列表数据
const userList = ref<User[]>([])
const loading = ref(false)

// 表格分页
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0,
})

// 获取用户列表
async function fetchUserList() {
  loading.value = true
  try {
    const res = await getUserList(1, 10)
    console.warn('API返回的用户数据:', res)

    // 处理API返回的数据
    if (res && Array.isArray(res)) {
      userList.value = res
      pagination.total = res.length
      console.warn('成功获取用户列表数据，共', userList.value.length, '条记录')
    }
    else if (res && typeof res === 'object' && Array.isArray(res.data)) {
      userList.value = res.data
      pagination.total = res.data.length
      console.warn('成功获取用户列表数据，共', userList.value.length, '条记录')
    }
    else {
      userList.value = []
      pagination.total = 0
      console.warn('API返回的数据格式不符合预期:', res)
    }
  }
  catch (error) {
    console.error('获取用户列表失败:', error)
    ElMessage.error('获取用户列表失败')
    userList.value = []
    pagination.total = 0
  }
  finally {
    loading.value = false
  }
}

// 处理分页变化
function handleCurrentChange(val: number) {
  pagination.currentPage = val
}

function handleSizeChange(val: number) {
  pagination.pageSize = val
  pagination.currentPage = 1
}

// 计算当前页的数据
const currentPageData = computed(() => {
  const start = (pagination.currentPage - 1) * pagination.pageSize
  const end = start + pagination.pageSize
  return userList.value.slice(start, end)
})

// 页面加载时获取用户列表
onMounted(() => {
  fetchUserList()
})

const waterMarkOptions = ref({
  text: '机密文件',
  fontSize: 24,
  density: 'normal',
})
</script>

<template>
  <div v-watermark="waterMarkOptions" class="user-manage-container">
    <div class="user-header">
      <h2 class="user-title">
        用户管理
      </h2>
    </div>

    <!-- 用户列表 -->
    <el-card shadow="hover" class="user-card">
      <el-table v-loading="loading" :data="currentPageData" stripe border style="width: 100%">
        <!-- 序号列 -->
        <el-table-column type="index" label="序号" width="60" />

        <!-- ID列 -->
        <el-table-column prop="userid" label="用户ID" min-width="180" show-overflow-tooltip />

        <!-- 用户名列 -->
        <el-table-column prop="username" label="用户名" min-width="120">
          <template #default="scope">
            {{ scope.row.username || '-' }}
          </template>
        </el-table-column>

        <!-- 昵称列 -->
        <el-table-column prop="nickname" label="昵称" min-width="120">
          <template #default="scope">
            {{ scope.row.nickname || '-' }}
          </template>
        </el-table-column>

        <!-- 头像列 -->
        <el-table-column label="头像" width="80">
          <template #default="scope">
            <el-avatar v-if="scope.row.avatar" :size="40" :src="scope.row.avatar" />
            <el-avatar v-else :size="40">
              {{ scope.row.username?.substring(0, 1).toUpperCase() || 'U' }}
            </el-avatar>
          </template>
        </el-table-column>

        <!-- 邮箱列 -->
        <el-table-column prop="email" label="邮箱" min-width="150" show-overflow-tooltip>
          <template #default="scope">
            {{ scope.row.email || '-' }}
          </template>
        </el-table-column>

        <!-- QQ列 -->
        <el-table-column prop="qq" label="QQ" min-width="120">
          <template #default="scope">
            {{ scope.row.qq || '-' }}
          </template>
        </el-table-column>

        <!-- 微信列 -->
        <el-table-column prop="wx" label="微信" min-width="120">
          <template #default="scope">
            {{ scope.row.wx || '-' }}
          </template>
        </el-table-column>

        <!-- 性别列 -->
        <el-table-column prop="sex" label="性别" width="80">
          <template #default="scope">
            <span v-if="scope.row.sex === 1">男</span>
            <span v-else-if="scope.row.sex === 0">女</span>
            <span v-else-if="scope.row.sex === -1">未知</span>
            <span v-else>-</span>
          </template>
        </el-table-column>

        <!-- 生日列 -->
        <el-table-column prop="birthday" label="生日" min-width="120">
          <template #default="scope">
            {{ scope.row.birthday || '-' }}
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.currentPage" v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]" layout="total, sizes, prev, pager, next, jumper" :total="pagination.total"
          @size-change="handleSizeChange" @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<style scoped lang="scss">
.user-manage-container {
  padding: 20px;

  .user-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;

    .user-title {
      font-size: 20px;
      font-weight: 600;
      margin: 0;
    }
  }

  .user-card {
    margin-bottom: 20px;
  }

  .pagination-container {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>
